﻿/****************************** Module Header ******************************\
 * Module Name:  CnRegionPicker.cs
 * Module Version:  1.0.0 (2012-10-17)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This componse is use for pick the chinese region.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UCL.Web.CustomControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CnRegionPicker runat=server></{0}:CnRegionPicker>")]
    public class CnRegionPicker : CompositeControl
    {
        private HtmlSelect ddlProvince;
        private HtmlSelect ddlCity;
        private HtmlSelect ddlArea;
        private HiddenField hidProvince;
        private HiddenField hidCity;
        private HiddenField hidArea;

        #region 公开属性
        
        /// <summary>
        /// 省份名称
        /// </summary>
        public string ProvinceText
        {
            get { EnsureChildControls(); return hidProvince.Value; }
            set { EnsureChildControls(); hidProvince.Value = value; }
        }

        /// <summary>
        /// 城市名称
        /// </summary>
        public string CityText
        {
            get { EnsureChildControls(); return hidCity.Value; }
            set { EnsureChildControls(); hidCity.Value = value; }
        }

        /// <summary>
        /// 地区名称
        /// </summary>
        public string AreaText
        {
            get { EnsureChildControls(); return hidArea.Value; }
            set { EnsureChildControls(); hidArea.Value = value; }
        }
        #endregion

        protected override void CreateChildControls()
        {
            ddlProvince = new HtmlSelect();
            ddlCity = new HtmlSelect();
            ddlArea = new HtmlSelect();
            hidProvince = new HiddenField();
            hidCity = new HiddenField();
            hidArea = new HiddenField();
            this.Controls.Add(ddlProvince);
            this.Controls.Add(ddlCity);
            this.Controls.Add(ddlArea);
            this.Controls.Add(hidProvince);
            this.Controls.Add(hidCity);
            this.Controls.Add(hidArea);
        }
        protected override void OnPreRender(EventArgs e)
        {
            Page.PreRenderComplete += PagePreRenderComplete;
            base.OnPreRender(e);
        }
        private void PagePreRenderComplete(object s, EventArgs e)
        {
            string script = "function SetCnRegion($a,$b,$c,$av,$bv,$cv){var c='<option>请选择</option>';$a.html(c);$b.html(c);$c.html(c);c='/DMS/Apis/o/apiGetCnRegion.ashx';$a.load(c,{dataType:'option',filter:'province'},function(){var $t=$(this);$t.width($t.width());if($av.val())$(this).children('option[value=\"'+$av.val()+'\"]').attr('selected','true')});if($av.val())$b.load(c,{dataType:'option',filter:'city',province:$av.val()},function(){var $t=$(this);$t.width($t.width());if($bv.val())$t.children('option[value=\"'+$bv.val()+'\"]').attr('selected','true')});if($av.val()&&$bv.val())$c.load(c,{dataType:'option',filter:'area',province:$av.val(),city:$bv.val()},function(){var $t=$(this);$t.width($t.width());if($cv.val())$t.children('option[value=\"'+$cv.val()+'\"]').attr('selected','true')});$a.change(function(){$av.val($a.val());$b.load(c,{dataType:'option',filter:'city',province:$a.find('option:selected').text()},function(){var $t=$(this);$t.width($t.width())})});$b.change(function(){$bv.val($b.val());$c.load(c,{dataType:'option',filter:'area',province:$a.find('option:selected').text(),city:$b.find('option:selected').text()},function(){var $t=$(this);$t.width($t.width())})});$c.change(function(){$cv.val($c.val())})};";
            string run = string.Format("SetCnRegion1($('select[name=\"{0}\"]'),$('select[name=\"{1}\"]'),$('select[name=\"{2}\"]'),$('input[name=\"{3}\"]'),$('input[name=\"{4}\"]'),$('input[name=\"{5}\"]'));", ddlProvince.UniqueID, ddlCity.UniqueID, ddlArea.UniqueID,hidProvince.UniqueID,hidCity.UniqueID,hidArea.UniqueID);
            Page.ClientScript.RegisterStartupScript(typeof(Page), "SetCnRegion", script, true);
            Page.ClientScript.RegisterStartupScript(typeof(Page), this.ID, run, true);
        }
        protected override void Render(HtmlTextWriter writer)
        {
            foreach (Control c in this.Controls)
            {
                Page.ClientScript.RegisterForEventValidation(c.UniqueID);
                c.RenderControl(writer);
            }
        }
    }
}
